package com.sky.controller.user;


import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/shoppingCart")
@Slf4j
@Api(tags = "微信購物車接口")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加購物車
     * @param shoppingCartDTO
     * @return
     */
    @ApiOperation("添加購物車")
    @PostMapping("/add")
    public Result<String> add(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("添加購物車：{}", shoppingCartDTO);
        Long id = BaseContext.getCurrentId();
        log.info("用戶id：{}", id);

        shoppingCartService.add(shoppingCartDTO);

        return Result.success();
    }

    /**
     * 查看購物車
     * @param
     * @return
     */
    @GetMapping("/list")
    public Result<List<ShoppingCart>> select() {
        log.info("查看購物車：");
        return Result.success(shoppingCartService.select());
    }

    /**
     * 單個刪除
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/sub")
    public Result delete(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("刪除購物車中單個商品：{}", shoppingCartDTO);
        shoppingCartService.delete(shoppingCartDTO);
        return Result.success();
    }

    @DeleteMapping("/clean")
    @ApiOperation("清空购物车商品")
    public Result<String> clean() {
        log.info("清空購物車：");
        shoppingCartService.cleanShoppingCart();
        return Result.success();
    }
}
